﻿using System;
using System.Data;
using System.Data.OleDb;

namespace DbManager.DataManager
{
    public class DataManagerOleDb : IDataManager
    {
        public DataTable GetDataTable(string queryString, IDbConnection dbConnection)
        {
            if (string.IsNullOrEmpty(queryString) || dbConnection == null)
            {
                return null;
            }

            var dataSet = new DataSet();
            using (dbConnection)
            {
                var dataAdapter = new OleDbDataAdapter(queryString,(OleDbConnection) dbConnection);
                LoggerLibrary.LogManager.WriteInfoToLog(string.Format("Выполняю запрос: {0}", queryString));
                dataAdapter.Fill(dataSet);
            }
            return dataSet.Tables[0];
        }

        public int ManageDataInDb(string queryString, IDbConnection dbConnection)
        {
            if (string.IsNullOrEmpty(queryString) || dbConnection == null)
            {
                return 0;
            }

            using (dbConnection)
            {
                var command = new OleDbCommand(queryString, (OleDbConnection) dbConnection)
                                  {CommandType = CommandType.Text};
                try
                {
                    LoggerLibrary.LogManager.WriteInfoToLog(string.Format("Выполняю запрос: {0}", queryString));
                    return command.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    LoggerLibrary.LogManager.WriteErrorToLog("Ошибка выполнения запроса.");
                    return 0;
                }
                
            }
        }

        public int GetNewId(string queryString, IDbConnection dbConnection)
        {
            if (dbConnection == null) return 0;

            using (dbConnection)
            {
                var command = new OleDbCommand(queryString, (OleDbConnection)dbConnection) { CommandType = CommandType.Text };
                try
                {
                    var dataReader = command.ExecuteReader();
                    LoggerLibrary.LogManager.WriteInfoToLog(string.Format("Выполняю запрос: {0}", queryString));
                    dataReader.Read();
                    if (dataReader.IsDBNull(0))
                    {
                        return 1;
                    }
                    return ((int)dataReader.GetValue(0)) + 1;
                }
                catch (Exception)
                {
                    LoggerLibrary.LogManager.WriteErrorToLog("Ошибка выполнения запроса.");
                    return 0;
                }

            }
        }

        public string GetItemStringById(string queryString, IDbConnection dbConnection)
        {
            if (dbConnection == null) return string.Empty;

            using (dbConnection)
            {
                var command = new OleDbCommand(queryString, (OleDbConnection)dbConnection) { CommandType = CommandType.Text };
                try
                {
                    var dataReader = command.ExecuteReader();
                    LoggerLibrary.LogManager.WriteInfoToLog(string.Format("Выполняю запрос: {0}", queryString));
                    dataReader.Read();
                    return dataReader.GetValue(0).ToString();
                }
                catch (Exception)
                {
                    LoggerLibrary.LogManager.WriteErrorToLog("Ошибка выполнения запроса.");
                    return string.Empty;
                }

            }
        }
    }
}